Use case: MPU configuration

Note

This configuration is applicable with ARMv7-M and ARMv6-M architectures.

Sequence diagram

MPU configuration (ARMv7-M and ARMv6-M)

  @startuml

  group Configure MPU
  "User Application"->"Cortex Driver" : HAL_MPU_Disable()
  group Configure Memory
  "User Application"->"Cortex Driver" : HAL_MPU_ConfigRegion()
  "Cortex Driver"-->"User Application"
  end
  "User Application"->"Cortex Driver" : HAL_MPU_Enable()
  end

  @enduml

Code snippet

This use case demonstrates how to configure a region of the memory (RAM for example) with read/write access with MPU using HAL1 compared to HAL2.

HAL1

HAL2

MPU_Region_InitTypeDef MPU_InitStruct;

/* Disable MPU */
HAL_MPU_Disable();

/* Configure RAM region as Region Number 0, 64KB of size and
   Read only in privileged mode */
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.BaseAddress = EXAMPLE_RAM_ADDRESS_START;
MPU_InitStruct.Size = MPU_REGION_SIZE_64KB;
MPU_InitStruct.AccessPermission = MPU_REGION_PRIV_RO;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
MPU_InitStruct.SubRegionDisable = 0x00;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;

HAL_MPU_ConfigRegion(&MPU_InitStruct);

HAL_MPU_Enable();
hal_cortex_mpu_region_config_t set_region;

/* Disable MPU before preloading and config update */
HAL_CORTEX_MPU_Disable();

/* Configure RAM region as Region Number 0, 64KB of size
   and Read only in privileged mode */
set_region.base_addr   = EXAMPLE_RAM_ADDRESS_START;
set_region.size        = HAL_CORTEX_MPU_REGION_SIZE_64KB;
set_region.access_attr = HAL_CORTEX_MPU_REGION_ALL_RW;
set_region.mem_attr    = HAL_CORTEX_MPU_NORMAL_MEM_NSHAREABLE_IO_NCACHEABLE;
set_region.exec_attr   = HAL_CORTEX_MPU_EXECUTION_ATTR_ENABLE;
set_region.sub_region  = 0x00;

hal_status_t state = HAL_CORTEX_MPU_SetConfigRegion(HAL_CORTEX_MPU_REGION_0,
                                                    &set_region);
HAL_CORTEX_MPU_EnableRegion(HAL_CORTEX_MPU_REGION_0);

HAL_CORTEX_MPU_Enable();

Key changes

  1. In HAL2, MPU APIs are renamed with the HAL_CORTEX_ prefix.

  2. In HAL2, MPU region configuration uses a dedicated region configuration structure.